//
//  ZZHomeRetweetView.swift
//  web
//
//  Created by 张卓 on 2017/2/4.
//  Copyright © 2017年 ZZ. All rights reserved.
//

/// 转发微博

import UIKit

class ZZHomeRetweetView: UIView {
    
    //记录约束
    var retweetViewConstraint: Constraint?
    
    //提供属性供外界赋值
    var statusViewModel:ZZStatusViewModel? {
        didSet{
         contentLabel.text = statusViewModel?.homeModel?.retweeted_status?.text
            //判断配图
            //图片
            //            print(statuViewModel?.homeModel?.pic_urls)
            // 卸载记录约束
            retweetViewConstraint?.uninstall()
            // 图片不为nil. 且 个数 大于 0
            if let pic = statusViewModel?.homeModel?.retweeted_status?.pic_urls,pic.count > 0 {
                //有值
                //赋值
                pictureView.pic_urls = pic
                
                //设置约束
                snp_makeConstraints(closure: { (make) in
                    retweetViewConstraint =  make.bottom.equalTo(pictureView).offset(10).constraint
                })
                //显示图片
                pictureView.isHidden = false
            }else{
                //没有值
                //设置约束
                snp_makeConstraints(closure: { (make) in
                    retweetViewConstraint =  make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                //隐藏图片
                pictureView.isHidden = true
            }
            
            
        }
    }

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置视图
    private func setupUI(){
        // 随机颜色
        backgroundColor = RGB(r: 235, g: 235, b: 235)
        
        // 01 添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        // 02 添加约束
        contentLabel.snp_makeConstraints { (make) in
            make.left.top.equalTo(10)
            make.width.equalTo(ZZSCREENW - 20)
        }
        pictureView.snp_makeConstraints { (make) in
            make.left.equalTo(10)
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
        }
        
        // 约束转发微博的底部
        snp_makeConstraints { (make) in
           retweetViewConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        
        // 测试数据
//        contentLabel.text = "测试数据测试数据测试数据测试数w据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据"
    }
    
    //MARK: - 懒加载控件
    // 内容
    private lazy var contentLabel: UILabel = {
        let lab = UILabel(fontSize: ZZNORMALFONTSIZE, textColor: UIColor.darkGray)
        // 换行
        lab.numberOfLines = 0
        // 背景颜色
//        lab.backgroundColor = UIColor.white
        return lab
    }()
    // 配图
    private lazy var pictureView: ZZHomePictureView = {
       let view = ZZHomePictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
    
}
